//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    "carouselList": [{
        "id": "101",
        "img": "/images/banner/蔬菜2.jpg",
        "title": "",
        "url": "https://www.baidu.com/"
      },
      {
        "id": "102",
        "img": "/images/banner/b3.jpg",
        "title": "百度翻译",
        "url": "https://fanyi.baidu.com/"
      },
      {
        "id": "102",
        "img": "/images/banner/green.jpg",
        "title": "百度翻译",
        "url": "https://fanyi.baidu.com/"
      },
      {
        "id": "103",
        "img": "/images/banner/guimo.jpg",
        "title": "百度地图",
        "url": "https://map.baidu.com/"
      },
      {
        "id": "104",
        "img": "/images/banner/guimo2.jpg",
        "title": "简书是一个写博客的网站，挺好用的，可以试试看",
        "url": "https://www.jianshu.com/"
      }
    ],
    products: [],
    windowHeight: 0,
    windowWidth: 0,
    showModalStatus: false, //是否显示
    gg_id: 0, //规格ID
    gg_txt: '', //规格文本
    gg_price: 0, //规格价格
    guigeList: [{
      guige: '100',
      price: '150'
    }, {
      guige: '200',
      price: '150'
    }, {
      guige: '300',
      price: '150'
    }],
    num: 500 //初始数量
  },

  //对于需要授权登录的操作进行授权弹窗
  onReady: function () {
    //获得popup组件
    this.popup = this.selectComponent("#popup");
    //判断是否登录，有全局openId
    if (app.globalData.openId) {
      console.info("已经从全局数据中获取到openId");
      console.info(app.globalData.openId);
      return;
    } else {
      console.info("未获取到全局id，弹窗授权");
      console.info(app.globalData.openId);
      this.showPopup();
      return;
    }
  },
  showPopup: function () {
    this.popup.showPopup();
  },

  onLoad: function () {

  },

  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })

    var that = this;
    wx.request({
      url: 'https://www.goup-open.cn/mini/product/get',
      method: 'GET', //方法分GET和POST，根据需要写
      header: { //定死的格式
        'Content-Type': 'application/json'
      },
      success: function (res) { //这里写调用接口成功之后所运行的函数
        //console.log(res.data.data);//调出来的数据在控制台显示，方便查看
        that.setData({
          products: res.data.data, //res.data就是你调出来的所有数据（当然也可以在这里面自定义想要调用的数据），然后把值赋给resdata，之后对resdata进行处理即可，具体见wxml
        })
      },
      fail: function (res) { //这里写调用接口失败之后所运行的函数
        console.log('.........fail..........');
      }
    })

  },
  freeTell: function () {
    wx.makePhoneCall({
      phoneNumber: '13240000522'
    })
  },
  //全局用户信息
  userInfo: function () {

  },


  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 100) {
      num = num - 500;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    num = num + 500;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },

  //显示对话框
  showModal: function (event) {
    //判断是否登录，有全局openId
    if (app.globalData.openId) {
      console.info("已经从全局数据中获取到openId");
      console.info(app.globalData.openId);
    } else {
      console.info("未获取到全局id，弹窗授权");
      console.info(app.globalData.openId);
      this.showPopup();
      return;
    }
    console.info(event);
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true,
      num: 500,
      buy_img: event.currentTarget.dataset.img,
      buy_id: event.currentTarget.dataset.id,
      buy_price: event.currentTarget.dataset.price,
      buy_pull_time: event.currentTarget.dataset.time,
      buy_name: event.currentTarget.dataset.name,
      buy_total_amount: event.currentTarget.dataset.total,
      buy_merchant: event.currentTarget.dataset.merchant
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  createOrder: function (e) {
    var productId = this.data.buy_id;
    var price = this.data.buy_price;
    var num = this.data.num;
    var totalAmount = price * num;
    var openId = app.globalData.openId;
    var pullTime = this.data.buy_pull_time;
    var address = "海城市温香镇丁家村";
    var merchantCode = this.data.buy_merchant;
    var phone = e.detail.value.phone;
    var name = e.detail.value.name;
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        duration: 3500
      })
      return;
    }

    if(name == ""){
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none',
        duration: 3500
      })
      return;
    }
    wx.request({
      url: 'https://www.goup-open.cn/mini/order/create',
      data: {
        'productId': productId,
        'price': price,
        'num': num,
        'totalAmount': totalAmount,
        'openId': openId,
        'address': address,
        'pullTime': pullTime,
        'merchantCode': merchantCode,
        'phone':phone,
        'name':name
      },
      method: 'POST', //方法分GET和POST，根据需要写
      header: { //定死的格式
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) { //这里写调用接口成功之后所运行的函数
        if (res.data.code == 200) {
          wx.showToast({
            title: '下单成功！', // 标题
            icon: 'success', // 图标类型，默认success
            duration: 3500 // 提示窗停留时间，默认1500ms
          })
        } else {
          wx.showToast({
            title: '下单失败',
            icon: 'none',
            duration: 3500
          })
        }
      },
      fail: function (res) { //这里写调用接口失败之后所运行的函数
        wx.showToast({
          title: '下单失败',
          icon: 'none',
          duration: 3500
        })
      }
    })
    //隐藏下单弹窗
    this.hideModal();
  }
})